{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solution Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem: Implement a linked list with insert, append, find, delete, length, and print methods.\n",
    "\n",
    "* [Constraints](#Constraints)\n",
    "* [Test Cases](#Test-Cases)\n",
    "* [Algorithm](#Algorithm)\n",
    "* [Code](#Code)\n",
    "* [Unit Test](#Unit-Test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Constraints\n",
    "\n",
    "* Can we assume this is a non-circular, singly linked list?\n",
    "    * Yes\n",
    "* Do we keep track of the tail or just the head?\n",
    "    * Just the head\n",
    "* Can we insert None values?\n",
    "    * No"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test Cases\n",
    "\n",
    "### Insert to Front\n",
    "\n",
    "* Insert a None\n",
    "* Insert in an empty list\n",
    "* Insert in a list with one element or more elements\n",
    "\n",
    "### Append\n",
    "\n",
    "* Append a None\n",
    "* Append in an empty list\n",
    "* Insert in a list with one element or more elements\n",
    "\n",
    "### Find\n",
    "\n",
    "* Find a None\n",
    "* Find in an empty list\n",
    "* Find in a list with one element or more matching elements\n",
    "* Find in a list with no matches\n",
    "\n",
    "### Delete\n",
    "\n",
    "* Delete a None\n",
    "* Delete in an empty list\n",
    "* Delete in a list with one element or more matching elements\n",
    "* Delete in a list with no matches\n",
    "\n",
    "### Length\n",
    "\n",
    "* Length of zero or more elements\n",
    "\n",
    "### Print\n",
    "\n",
    "* Print an empty list\n",
    "* Print a list with one or more elements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Algorithm\n",
    "\n",
    "### Insert to Front\n",
    "\n",
    "* If the data we are inserting is None, return\n",
    "* Create a node with the input data, set node.next to head\n",
    "* Assign the head to the node\n",
    "\n",
    "Complexity:\n",
    "* Time: O(1)\n",
    "* Space: O(1)\n",
    "\n",
    "### Append\n",
    "\n",
    "* If the data we are inserting is None, return\n",
    "* Create a node with the input data\n",
    "* If this is an empty list\n",
    "    * Assign the head to the node\n",
    "* Else\n",
    "    * Iterate to the end of the list\n",
    "    * Set the final node's next to the new node\n",
    "\n",
    "Complexity:\n",
    "* Time: O(n)\n",
    "* Space: O(1)\n",
    "\n",
    "### Find\n",
    "\n",
    "* If data we are finding is None, return\n",
    "* If the list is empty, return\n",
    "* For each node\n",
    "    * If the value is a match, return it\n",
    "    * Else, move on to the next node\n",
    "\n",
    "Complexity:\n",
    "* Time: O(n)\n",
    "* Space: O(1)\n",
    "\n",
    "### Delete\n",
    "\n",
    "* If data we are deleting is None, return\n",
    "* If the list is empty, return\n",
    "* For each node, keep track of previous and current node\n",
    "    * If the value we are deleting is a match in the current node\n",
    "        * Update previous node's next pointer to the current node's next pointer\n",
    "        * We do not have have to explicitly delete in Python\n",
    "    * Else, move on to the next node\n",
    "* As an alternative, we could avoid the use of two pointers by evaluating the current node's next value:\n",
    "    * If the next value is a match, set the current node's next to next.next\n",
    "    * Special care should be taken if deleting the head node\n",
    "\n",
    "Complexity:\n",
    "* Time: O(n)\n",
    "* Space: O(1)\n",
    "\n",
    "### Length\n",
    "\n",
    "* For each node\n",
    "    * Increase length counter\n",
    "    \n",
    "Complexity:\n",
    "* Time: O(n)\n",
    "* Space: O(1)\n",
    "\n",
    "### Print\n",
    "\n",
    "* For each node\n",
    "    * Print the node's value\n",
    "    \n",
    "Complexity:\n",
    "* Time: O(n)\n",
    "* Space: O(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting linked_list.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile linked_list.py\n",
    "class Node(object):\n",
    "\n",
    "    def __init__(self, data, next=None):\n",
    "        self.next = next\n",
    "        self.data = data\n",
    "\n",
    "    def __str__(self):\n",
    "        return self.data\n",
    "\n",
    "\n",
    "class LinkedList(object):\n",
    "\n",
    "    def __init__(self, head=None):\n",
    "        self.head = head\n",
    "\n",
    "    def __len__(self):\n",
    "        curr = self.head\n",
    "        counter = 0\n",
    "        while curr is not None:\n",
    "            counter += 1\n",
    "            curr = curr.next\n",
    "        return counter\n",
    "\n",
    "    def insert_to_front(self, data):\n",
    "        if data is None:\n",
    "            return None\n",
    "        node = Node(data, self.head)\n",
    "        self.head = node\n",
    "        return node\n",
    "\n",
    "    def append(self, data):\n",
    "        if data is None:\n",
    "            return None\n",
    "        node = Node(data)\n",
    "        if self.head is None:\n",
    "            self.head = node\n",
    "            return node\n",
    "        curr_node = self.head\n",
    "        while curr_node.next is not None:\n",
    "            curr_node = curr_node.next\n",
    "        curr_node.next = node\n",
    "        return node\n",
    "\n",
    "    def find(self, data):\n",
    "        if data is None:\n",
    "            return None\n",
    "        curr_node = self.head\n",
    "        while curr_node is not None:\n",
    "            if curr_node.data == data:\n",
    "                return curr_node\n",
    "            curr_node = curr_node.next\n",
    "        return None\n",
    "\n",
    "    def delete(self, data):\n",
    "        if data is None:\n",
    "            return\n",
    "        if self.head is None:\n",
    "            return\n",
    "        if self.head.data == data:\n",
    "            self.head = self.head.next\n",
    "            return\n",
    "        prev_node = self.head\n",
    "        curr_node = self.head.next\n",
    "        while curr_node is not None:\n",
    "            if curr_node.data == data:\n",
    "                prev_node.next = curr_node.next\n",
    "                return\n",
    "            prev_node = curr_node\n",
    "            curr_node = curr_node.next\n",
    "\n",
    "    def delete_alt(self, data):\n",
    "        if data is None:\n",
    "            return\n",
    "        if self.head is None:\n",
    "            return\n",
    "        curr_node = self.head\n",
    "        if curr_node.data == data:\n",
    "            curr_node = curr_node.next\n",
    "            return\n",
    "        while curr_node.next is not None:\n",
    "            if curr_node.next.data == data:\n",
    "                curr_node.next = curr_node.next.next\n",
    "                return\n",
    "            curr_node = curr_node.next\n",
    "\n",
    "    def print_list(self):\n",
    "        curr_node = self.head\n",
    "        while curr_node is not None:\n",
    "            print(curr_node.data)\n",
    "            curr_node = curr_node.next\n",
    "\n",
    "    def get_all_data(self):\n",
    "        data = []\n",
    "        curr_node = self.head\n",
    "        while curr_node is not None:\n",
    "            data.append(curr_node.data)\n",
    "            curr_node = curr_node.next\n",
    "        return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run linked_list.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unit Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting test_linked_list.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile test_linked_list.py\n",
    "import unittest\n",
    "\n",
    "\n",
    "class TestLinkedList(unittest.TestCase):\n",
    "\n",
    "    def test_insert_to_front(self):\n",
    "        print('Test: insert_to_front on an empty list')\n",
    "        linked_list = LinkedList(None)\n",
    "        linked_list.insert_to_front(10)\n",
    "        self.assertEqual(linked_list.get_all_data(), [10])\n",
    "\n",
    "        print('Test: insert_to_front on a None')\n",
    "        linked_list.insert_to_front(None)\n",
    "        self.assertEqual(linked_list.get_all_data(), [10])\n",
    "\n",
    "        print('Test: insert_to_front general case')\n",
    "        linked_list.insert_to_front('a')\n",
    "        linked_list.insert_to_front('bc')\n",
    "        self.assertEqual(linked_list.get_all_data(), ['bc', 'a', 10])\n",
    "\n",
    "        print('Success: test_insert_to_front\\n')\n",
    "\n",
    "    def test_append(self):\n",
    "        print('Test: append on an empty list')\n",
    "        linked_list = LinkedList(None)\n",
    "        linked_list.append(10)\n",
    "        self.assertEqual(linked_list.get_all_data(), [10])\n",
    "\n",
    "        print('Test: append a None')\n",
    "        linked_list.append(None)\n",
    "        self.assertEqual(linked_list.get_all_data(), [10])\n",
    "\n",
    "        print('Test: append general case')\n",
    "        linked_list.append('a')\n",
    "        linked_list.append('bc')\n",
    "        self.assertEqual(linked_list.get_all_data(), [10, 'a', 'bc'])\n",
    "\n",
    "        print('Success: test_append\\n')\n",
    "\n",
    "    def test_find(self):\n",
    "        print('Test: find on an empty list')\n",
    "        linked_list = LinkedList(None)\n",
    "        node = linked_list.find('a')\n",
    "        self.assertEqual(node, None)\n",
    "\n",
    "        print('Test: find a None')\n",
    "        head = Node(10)\n",
    "        linked_list = LinkedList(head)\n",
    "        node = linked_list.find(None)\n",
    "        self.assertEqual(node, None)\n",
    "\n",
    "        print('Test: find general case with matches')\n",
    "        head = Node(10)\n",
    "        linked_list = LinkedList(head)\n",
    "        linked_list.insert_to_front('a')\n",
    "        linked_list.insert_to_front('bc')\n",
    "        node = linked_list.find('a')\n",
    "        self.assertEqual(str(node), 'a')\n",
    "\n",
    "        print('Test: find general case with no matches')\n",
    "        node = linked_list.find('aaa')\n",
    "        self.assertEqual(node, None)\n",
    "\n",
    "        print('Success: test_find\\n')\n",
    "\n",
    "    def test_delete(self):\n",
    "        print('Test: delete on an empty list')\n",
    "        linked_list = LinkedList(None)\n",
    "        linked_list.delete('a')\n",
    "        self.assertEqual(linked_list.get_all_data(), [])\n",
    "\n",
    "        print('Test: delete a None')\n",
    "        head = Node(10)\n",
    "        linked_list = LinkedList(head)\n",
    "        linked_list.delete(None)\n",
    "        self.assertEqual(linked_list.get_all_data(), [10])\n",
    "\n",
    "        print('Test: delete general case with matches')\n",
    "        head = Node(10)\n",
    "        linked_list = LinkedList(head)\n",
    "        linked_list.insert_to_front('a')\n",
    "        linked_list.insert_to_front('bc')\n",
    "        linked_list.delete('a')\n",
    "        self.assertEqual(linked_list.get_all_data(), ['bc', 10])\n",
    "\n",
    "        print('Test: delete general case with no matches')\n",
    "        linked_list.delete('aa')\n",
    "        self.assertEqual(linked_list.get_all_data(), ['bc', 10])\n",
    "\n",
    "        print('Success: test_delete\\n')\n",
    "\n",
    "    def test_len(self):\n",
    "        print('Test: len on an empty list')\n",
    "        linked_list = LinkedList(None)\n",
    "        self.assertEqual(len(linked_list), 0)\n",
    "\n",
    "        print('Test: len general case')\n",
    "        head = Node(10)\n",
    "        linked_list = LinkedList(head)\n",
    "        linked_list.insert_to_front('a')\n",
    "        linked_list.insert_to_front('bc')\n",
    "        self.assertEqual(len(linked_list), 3)\n",
    "\n",
    "        print('Success: test_len\\n')\n",
    "\n",
    "\n",
    "def main():\n",
    "    test = TestLinkedList()\n",
    "    test.test_insert_to_front()\n",
    "    test.test_append()\n",
    "    test.test_find()\n",
    "    test.test_delete()\n",
    "    test.test_len()\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test: insert_to_front on an empty list\n",
      "Test: insert_to_front on a None\n",
      "Test: insert_to_front general case\n",
      "Success: test_insert_to_front\n",
      "\n",
      "Test: append on an empty list\n",
      "Test: append a None\n",
      "Test: append general case\n",
      "Success: test_append\n",
      "\n",
      "Test: find on an empty list\n",
      "Test: find a None\n",
      "Test: find general case with matches\n",
      "Test: find general case with no matches\n",
      "Success: test_find\n",
      "\n",
      "Test: delete on an empty list\n",
      "Test: delete a None\n",
      "Test: delete general case with matches\n",
      "Test: delete general case with no matches\n",
      "Success: test_delete\n",
      "\n",
      "Test: len on an empty list\n",
      "Test: len general case\n",
      "Success: test_len\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%run -i test_linked_list.py"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
